<?php

namespace builder\test\controllers;

use builder\base\BaseController;

//表单
class MenuController extends BaseController
{
    public $actionVerbs = ['m-index' => ['GET', 'POST'], 'mock' => ['GET', 'POST']];
    public $publicActions = ['m-index', 'mock'];

    public function actionMock()
    {
        return $this->asOk('Success');
    }

    //示例
    public function actionMIndex()
    {
        $viewBuilder = $this->viewBuilder;
        $viewBuilder->setPartial(!!@$this->get['_partial']);
        $menu = $viewBuilder->getMenu(['id' => 'Menu_Index_Menu']);
        $table = $viewBuilder->getTable(['id' => 'Menu_Index_Table']);
        if ($this->isAjax) {
            switch ($viewBuilder->builderId) {
                case $menu->id:
                    $menu->setData(function () {
                        return [
                            ['id' => 1, 'title' => '菜单1'],
                            ['id' => 2, 'title' => '菜单2'],
                            ['id' => 3, 'title' => '菜单3'],
                            ['id' => 4, 'title' => '菜单4'],
                            ['id' => 5, 'title' => '菜单5'],
                        ];
                    });
                    break;
                case $table->id:
                    $table->setPage(false)
                        ->setQuery(function () {
                            return [
                                ['id' => 1, 'name' => '字段1', 'desc' => '简述1'],
                                ['id' => 2, 'name' => '字段2', 'desc' => '简述2'],
                                ['id' => 3, 'name' => '字段3', 'desc' => '简述3'],
                                ['id' => 4, 'name' => '字段4', 'desc' => '简述4'],
                                ['id' => 5, 'name' => '字段5', 'desc' => '简述5'],
                                ['id' => 6, 'name' => '字段6', 'desc' => '简述6'],
                                ['id' => 7, 'name' => '字段7', 'desc' => '简述7'],
                            ];
                        })
                        ->setColumns([
                            'id',
                            'name',
                            'desc' => function ($row) {
                                return $row['desc'] ?: '-';
                            }
                        ]);
                    break;
            }
        } else {
            //分类菜单
            $menu->setRootLayout([
                'class' => ['w-230', 'flex-shrink-0'],
            ])
                ->setMenuToolbar([
                    $this->menuToolbar->modal()->title('添加分类')->btnClass('layui-btn-theme w-100')->route('ymtest/menu/mock')->titleMaxminBtnDisplay(),
                ])
                ->setRowEvents([
                    $this->rowEvents->modal()->title('编辑')->icon('layui-icon layui-icon-edit f20')->route('ymtest/menu/mock'),
                    $this->rowEvents->ajax()->title('删除')->icon('layui-icon layui-icon-delete f20')->route('ymtest/menu/mock')->method(),
                ])
                ->setBehaviorCall($table->id, ['category_id' => ':id']);
            //标签表格
            $table->setTitle('标签管理')
                ->setRootLayout([
                    'class' => ['flex-shrink-0'],
                    'style' => ['min-width' => '800px']
                ])
                ->setMaxHeight(760)
                ->setAutoRequest(false)
                ->setPage(false)
                ->setTableHead([
                    $this->tableHead->field('id')->title('ID')->sort(),
                    $this->tableHead->field('name')->title('标签')->width(300),
                    $this->tableHead->field('desc')->title('内容')->width(300),
                    $this->tableHead->toolbar()->title('操作项')->fixedNone()->width(200),
                ])
                ->setToolbarCustom([
                    $this->toolbarCustom->modal()->title('添加标签')->route('ymtest/menu/mock')->height(450),
                ])
                ->setToolbarExports()
                ->setToolbarPrint()
                ->setRowOperation([
                    $this->rowOperation->modal()->title('编辑')->route('ymtest/menu/mock')->height(450),
                    $this->rowOperation->ajax()->title('删除')->route('ymtest/menu/mock')->method('post'),
                ])
                ->setToolbarFilter([
                    'keyword' => $this->toolbarFilter->text()->label('关键词')->placeholder('请填写')->uiClass('w-330 pr-3'),
                ]);
        }

        return $viewBuilder->setAlignThemePath('@builder/test/views/layouts/row.php')->render();
    }
}